﻿using UnityEngine;
using System.IO;
using LitJson;

[RequireComponent(typeof(AudioSource))]
public class GameManager : Singleton<GameManager>
{
    private AudioClip Song_1;
    private AudioClip Song_2;
    private AudioClip Song_3;
    private BeatConfig BeatConfig_1;
    private BeatConfig BeatConfig_2;
    private BeatConfig BeatConfig_3;
    private BeatConfig BeatConfig_4;
    private BeatConfig currentSceneConfig;
    
    private AudioSource musicPlayer;
    private BeatManager beatManager;
    public float beatBeforeTriggerDuration_MS = 1000;

    public static bool IsSongCompleted = false;
    private void Awake()
    {
        beatManager = GetComponent<BeatManager>();
        musicPlayer = GetComponent<AudioSource>();
        musicPlayer.playOnAwake = false;
        Song_1 = Resources.Load<AudioClip>(Global.SONG_BASE_PATH + "1");
        Song_2 = Resources.Load<AudioClip>(Global.SONG_BASE_PATH + "2");
        Song_3 = Resources.Load<AudioClip>(Global.SONG_BASE_PATH + "3");

        StreamReader sr_1 = new StreamReader(Application.streamingAssetsPath + "/SongConfig_1.json");
        string jsonStr_1 = sr_1.ReadToEnd();
        JsonReader reader_1 = new JsonReader(jsonStr_1);
        BeatConfig_1 = JsonMapper.ToObject<BeatConfig>(reader_1);  

        StreamReader sr_2 = new StreamReader(Application.streamingAssetsPath + "/SongConfig_2.json");
        string jsonStr_2 = sr_2.ReadToEnd();
        JsonReader reader_2 = new JsonReader(jsonStr_2);
        BeatConfig_2 = JsonMapper.ToObject<BeatConfig>(reader_2); 

        StreamReader sr_3 = new StreamReader(Application.streamingAssetsPath + "/SongConfig_3.json");
        string jsonStr_3 = sr_3.ReadToEnd();
        JsonReader reader_3 = new JsonReader(jsonStr_3);
        BeatConfig_3 = JsonMapper.ToObject<BeatConfig>(reader_3); 

        StreamReader sr_4 = new StreamReader(Application.streamingAssetsPath + "/SongConfig_4.json");
        string jsonStr_4 = sr_4.ReadToEnd();
        JsonReader reader_4 = new JsonReader(jsonStr_4);
        BeatConfig_4 = JsonMapper.ToObject<BeatConfig>(reader_4); 
    
        IsSongCompleted = false;
        Debug.Log("BPM:" + BeatConfig_1.BPM);
        //DontDestroyOnLoad(gameObject);
        
    }

    private void Start()
    {
        ReInitData();

        StartGame();
    }

    void ReInitData()
    {
        
        if (musicPlayer.isPlaying) musicPlayer.Stop();
        IsSongCompleted = false;

        if (LoadingPanel._sceneToLoad == "GameScene_1")
        {
            musicPlayer.clip = Song_1;
            currentSceneConfig = BeatConfig_1;
            BeatManager.Instance.AutoStart_34();
        }
        if (LoadingPanel._sceneToLoad == "GameScene_2")
        {
            musicPlayer.clip = Song_2;
            currentSceneConfig = BeatConfig_2;
            BeatManager.Instance.AutoStart_34();
        }
        if (LoadingPanel._sceneToLoad == "GameScene_3")
        {
            musicPlayer.clip = Song_1;
            currentSceneConfig = BeatConfig_3;
            BeatManager.Instance.AutoStart_34();
            BeatManager.Instance.AutoStart_56();
        }
        if (LoadingPanel._sceneToLoad == "GameScene_4")
        {
            musicPlayer.clip = Song_3;
            currentSceneConfig = BeatConfig_4;
        }
    }

    void StartGame()
    {
        float timeOffset = beatBeforeTriggerDuration_MS / 1000.0f;
        InternalStartBeat(timeOffset, currentSceneConfig);
        Timer.Register(timeOffset, InternalStartSong, null, false, true);
    }

    void InternalStartBeat(float beatBeforeTriggerDuration, BeatConfig newBeatsConfig)
    {
        beatManager.StartSpawnBeat(beatBeforeTriggerDuration, newBeatsConfig);
    }

    void InternalStartSong()
    {
        musicPlayer.Play();

        if(musicPlayer.isPlaying)
        {
            float songDuration = musicPlayer.clip.length;
            Timer.Register(songDuration, CompleteSongEvent);
        }
    }

    public void CompleteSongEvent()
    {
        if(musicPlayer.isPlaying) musicPlayer.Stop();

        IsSongCompleted = true;
        Debug.Log("SongEnd");
    }


}
